#region Using Statements
using System;
using DirectZZT.Core;
using DirectZZT.Shared.Core;
using System.IO;
#endregion

namespace DirectZZT
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// Declares the timestamp when this program was started.
        /// </summary>
        internal static readonly DateTime Start = DateTime.Now;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // register configuration files
            ConfigurationManager.Register("Client", "Client.conf");
            ConfigurationManager.Register("Input", "Input.conf");
            // initialize type manager
            TypeManager.Instance.Initialize(null);

            // register default logging
            Logging.GetLogging().Initialize();
            Logging.GetLogging().RegisterListener(new DiagnosticsLoggingListener(true));

            // register script debugger logging (128 KB should be enough)
            Logging.GetLogging("script").Initialize();
            Logging.GetLogging("script").MaxFileSize = 128 * 1024;
            Logging.GetLogging("script").SetOutputFileName(Path.Combine(Logging.LogFileDirectory, "Scripts.log"));
            DirectZZT.Shared.Scripting.ScriptDebugger.EventOcurred += ScriptDebugger_EventOcurred;

#if WINDOWS
            DirectZZT.Forms.ScriptDebuggerWindow.Initialize();
#endif
            // run the game
            using (DirectZZTGame game = new DirectZZTGame())
            {
                game.Run();
            }
            
            // cleanup
            DirectZZT.Shared.Scripting.ScriptDebugger.EventOcurred -= ScriptDebugger_EventOcurred;

#if WINDOWS
            DirectZZT.Forms.ScriptDebuggerWindow.Shutdown();
#endif
            // save configuration
            ConfigurationManager.Shutdown();

        }

        private static void ScriptDebugger_EventOcurred(object sender, Shared.Scripting.ScriptDebugger.ScriptDebuggerEventEventArgs e)
        {
            Logging.GetLogging("script").LogFormat(LoggingMessageType.Info, "", "", "Script: {0}. Parameters: {1}", e.Script.DebugName, string.Join(";", e.Parameters));
        }
    }
#endif
}

